#coding:utf-8
import os
import logging
import sys


def myLogger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    curpath=cur_file_dir();
    print(curpath)
    handler = logging.FileHandler(os.path.join(curpath+'/log/', name + '.log'), 'a')
    fmt = '%(asctime)s - %(filename)s:%(lineno)s  - %(message)s'  
    formatter = logging.Formatter(fmt)  
    handler.setFormatter(formatter)     
    logger.addHandler(handler)
    return logger


#获取脚本文件的当前路径
def cur_file_dir():
    #获取脚本路径
    path = sys.path[0]
    #判断为脚本文件还是py2exe编译后的文件，如果是脚本文件，则返回的是脚本的目录，如果是py2exe编译后的文件，则返回的是编译后的文件路径
    #path=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    if os.path.isdir(path):
        return path
    elif os.path.isfile(path):
        return os.path.dirname(path)
    
def mylogg(msg):
    '''
    # 创建一个logger 
    logger = logging.getLogger('') 
    logger.setLevel(logging.DEBUG) 
     
    # 创建一个handler，用于写入日志文件 
    fh = logging.FileHandler(cur_file_dir()+'/log/myapp.log') 
    fh.setLevel(logging.DEBUG) 
     
    # 再创建一个handler，用于输出到控制台 
    ch = logging.StreamHandler() 
    ch.setLevel(logging.DEBUG) 
     
    # 定义handler的输出格式 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 
    fh.setFormatter(formatter) 
    ch.setFormatter(formatter) 
     
    # 给logger添加handler 
    #logger.addHandler(fh) 
    #logger.addHandler(ch) 
 
    logger.info(msg)
    '''
    logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(levelname)s %(message)s',
                datefmt='%Y-%m-%d %H:%M:%S',
                filename=cur_file_dir()+'/log/myapp.log',
                filemode='a')
    
    console = logging.StreamHandler()
    
    console.setLevel(logging.DEBUG)
    # 设置日志打印格式
    formatter = logging.Formatter('%(asctime)s  %(levelname)s %(message)s')
    console.set_name('a')
    console.setFormatter(formatter)
    # 将定义好的console日志handler添加到root logger
    logging.getLogger('').addHandler(console)

    logging.info(msg)
    #console.flush()
    logging.getLogger('').removeHandler(console)
    